package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Hoteles extends Activity {

	private ArrayList<Dictionary> Actividades;
	public ProgressDialog progressDialog;
	public ListView list;
	public Hoteles_Adapter adapter;
	public String putCAT_CVE = "1";
	public boolean Splash = false;
	private String putTITULO;
	public App_ImageLoader imageLoader;
	public ArrayList<Dictionary> BannersHoteles;
	public ArrayList<Dictionary> ActividadesBanner = null;
	protected String QRY_BUSCADOR = "select * from pois where cat_cve=1 order by poi_idr";
	private Intent intent;

	public void voidFinish(View V) {
		finish();

	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(Hoteles.this, MenuLateral.class);

		startActivity(intent);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_listview);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			try {

				this.putCAT_CVE = extras.getString("putCAT_CVE");

			} catch (Exception ex) {
			}

			this.putTITULO = extras.getString("PUT_TITULO");

		}

		TextView Titulo = (TextView) this.findViewById(id.tv_listView_Titulo);
		Titulo.setText(putTITULO);

		// @+id/tv_listview_buscar_titutlo2

		// TextView TituloBuscar = (TextView) this
		// .findViewById(id.tv_listview_buscar_titutlo2);
		// TituloBuscar.setText(putTITULO);

		final EditText buscar = (EditText) this
				.findViewById(id.et_buscar_buscar);

		ImageView buscador = (ImageView) this.findViewById(id.imageView1);
		buscador.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				QRY_BUSCADOR = "select a.*, b.cat_des, b.cat_des_en from pois a inner join categorias b on a.cat_cve = b.cat_cve where a.cat_cve = 1 and (upper(a.poi_nombre) like '%"
						+ buscar.getText().toString().toUpperCase()
						+ "%' OR upper(a.poi_desc) like '%"
						+ buscar.getText().toString().toUpperCase()
						+ "%' OR upper(a.poi_desc_en) like '%"
						+ buscar.getText().toString().toUpperCase() + "%')";

				CargarContenido();
			}
		});

		CargarContenido();
		abrirSplashBanner();

	}

	public void abrirSplashBanner() {
		/** se obtiene datos **/

		Intent intent = new Intent(this, Splash.class);
		intent.putExtra("PUT_IMG_WEB", "HOTELES");
		startActivity(intent);

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	/**
	 * select * from banners WHERE BAN_TIPO IN ( 'BANNER HOTELES 1','BANNER
	 * HOTELES 2','BANNER HOTELES 3') ORDER BY ban_idr
	 * 
	 * 
	 * **/

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			list = (ListView) findViewById(R.id.list);
			adapter = new Hoteles_Adapter(ActividadesBanner);
			list.setAdapter(adapter);

			list.setTextFilterEnabled(true);

			list.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					try {
						if (ActividadesBanner.get(position).get("ban_tipo")
								.toString().trim().length() > 0) {

							Intent intent = new Intent(Hoteles.this,
									PatrocinadoresDetalle.class);

							clsDictionary.setDctionary(ActividadesBanner
									.get(position));

							startActivity(intent);
						}
					} catch (Exception e) {

						Intent intent = new Intent(Hoteles.this,
								ProveedoresPreferencialesDetalle1.class);

						clsDictionary.setDctionary(ActividadesBanner
								.get(position));

						startActivity(intent);

						// finish();
					}
				}
			});
			progressDialog.dismiss();

			// abrirSplashBanner();
		}

		protected void onPreExecute() {

			progressDialog = ProgressDialog.show(Hoteles.this, "",
					"Cargando...");
		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode(QRY_BUSCADOR);

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from banners WHERE BAN_TIPO IN ( 'BANNER HOTELES 1','BANNER HOTELES 2','BANNER HOTELES 3') ORDER BY ban_idr");

			saxparser = new saxXML(qry_url);

			BannersHoteles = saxparser.parse();

			// // 3 - 6 -9 -12
			//
			// if (position % 3 == 0) {
			// /** Insert banner **/
			//
			//
			// } else {

			ActividadesBanner = new ArrayList<Dictionary>();

			if (BannersHoteles.size() > 0) {

				int cont = 0;

				for (int i = 0; i < Actividades.size(); i++) {

					if (i > 0) {

						if (i % 2 == 0) {
							/** Insert banner **/
							// if (i > 0) {
							try {
								ActividadesBanner.add(BannersHoteles.get(cont));

								cont++;
								if (cont == 3)
									cont = 0;

								// }

							} catch (Exception e) {
								cont = 0;
								ActividadesBanner.add(BannersHoteles.get(cont));
							}

						}
					}

					ActividadesBanner.add(Actividades.get(i));

				}
			} else {

				ActividadesBanner = Actividades;
			}

			return null;

		}
	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) Hoteles.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			imageLoader = new App_ImageLoader(
					Hoteles.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {
			public TextView tvTitulo;
			public TextView tvSubtitulo;
			public ImageView icon;
			public ImageView vermas;

			public ImageView banner;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_listview_items, null);

				holder = new ViewHolder();

				holder.tvTitulo = (TextView) vi
						.findViewById(R.id.tv_listview_items_titulo);

				holder.tvSubtitulo = (TextView) vi
						.findViewById(R.id.tv_listview_items_subtitulo);

				// @+id/iv_listview_items_imagen
				holder.icon = (ImageView) vi
						.findViewById(id.iv_listview_items_imagen);

				holder.banner = (ImageView) vi
						.findViewById(id.iv_listview_items_banner);

				holder.vermas = (ImageView) vi.findViewById(id.iv_items_vermas);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			/** Insert banner **/
			// ActividadesBanner.add(BannersHoteles.get(0));
			// ban_url

			if (data.get(position).get("ban_url") != null) {

				holder.tvTitulo.setText("");

				holder.tvSubtitulo.setText("");

				try {
					String url_img = getString(R.string.QRY_URL_IMG)
							+ data.get(position).get("ban_url").toString()
									.replace(" ", "%20");

					holder.banner.setTag(url_img);

					imageLoader.DisplayImage(url_img, Hoteles.this,
							holder.banner);

					holder.icon.setVisibility(holder.icon.GONE);

					holder.vermas.setVisibility(holder.vermas.GONE);
					holder.banner.setVisibility(holder.icon.VISIBLE);

				} catch (Exception ex) {

					String s = ex.getMessage().toString();

				}

			} else {

				holder.banner.setVisibility(holder.icon.GONE);

				holder.icon.setVisibility(holder.icon.VISIBLE);

				// holder.vermas.setVisibility(holder.vermas.VISIBLE);

				String Nombre = data.get(position).get("poi_nombre").toString();
				holder.tvTitulo.setText(Nombre);

				String Direccion = data.get(position).get("poi_direccion")
						.toString();
				holder.tvSubtitulo.setText(Direccion);

				String url_img = getString(R.string.QRY_URL_IMG)
						+ data.get(position).get("poi_thumb").toString()
								.replace(" ", "%20");

				holder.icon.setTag(url_img);
				try {

					imageLoader
							.DisplayImage(url_img, Hoteles.this, holder.icon);

				} catch (Exception ex) {

					String s = ex.getMessage().toString();

				}

			}

			return vi;

		}

	}
}
